package com.firebird.web.rmi.impl;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.firebird.authority.FirebirdAuthorityService;
import com.firebird.core.auth.domain.LoginUser;
import com.firebird.core.sql.result.DataResult;
import com.firebird.doc.domain.ex.DocEntire;
import com.firebird.doc.domain.ex.TypeBrief;
import com.firebird.doc.server.FirebirdDocManagerInter;
import com.firebird.doc.server.FirebirdDocOperateRightInter.POP_TYPE;
import com.firebird.doc.server.FirebirdDocTypeInter;
import com.firebird.doc.server.FirebirdFileIndexManagerInter;
import com.firebird.doc.server.FirebirdFileManagerInter;
import com.firebird.util.spring.BeanFactory;
import com.firebird.fekp.api.FekpAppInter;
import com.firebird.fekp.api.exception.DocCreatErrorExcepiton;
import com.firebird.fekp.domain.Results;
import com.firebird.fekp.know.service.KnowServiceInter;

public class FekpAppImpl extends UnicastRemoteObject implements FekpAppInter {
	public FekpAppImpl() throws RemoteException {
		super();
	}

	private FirebirdFileIndexManagerInter firebirdFileIndexManagerImpl = (FirebirdFileIndexManagerInter) BeanFactory
			.getBean("firebirdFileIndexManagerImpl");

	private FirebirdFileManagerInter firebirdFileManagerImpl = (FirebirdFileManagerInter) BeanFactory
			.getBean("firebirdFileManagerImpl");

	private FirebirdDocManagerInter firebirdDocManagerImpl = (FirebirdDocManagerInter) BeanFactory.getBean("firebirdDocManagerImpl");
	private KnowServiceInter KnowServiceImpl = (KnowServiceInter) BeanFactory.getBean("knowServiceImpl");

	private FirebirdDocTypeInter firebirdDocTypeManagerImpl = (FirebirdDocTypeInter) BeanFactory.getBean("firebirdDocTypeManagerImpl");

	private static final long serialVersionUID = 1L;

	@Override
	public String creatHTMLKnow(String knowtitle, String knowtypeId, String text, String knowtag, String currentUserId)
			throws RemoteException, DocCreatErrorExcepiton {
		DocEntire doc = null;
		try {
			doc = KnowServiceImpl.creatKnow(knowtitle, knowtypeId, text, knowtag, POP_TYPE.PUB, POP_TYPE.PUB, null,
					null);
		} catch (Exception e) {
			throw new DocCreatErrorExcepiton(e);
		}
		return doc.getDoc().getId();
	}

	@Override
	public Results getTypeDocs(String typeid, int pagesize, int currentpage, String loginname) throws RemoteException {
		LoginUser user = FirebirdAuthorityService.getInstance().getUserByLoginName(loginname);
		DataResult docs = firebirdDocTypeManagerImpl.getTypeDocs(user, typeid, pagesize, currentpage);
		Results result = new Results();
		result.setList(docs.getResultList());
		result.setCurrentpage(currentpage);
		result.setPagesize(pagesize);
		result.setTotalsize(docs.getTotalSize());
		return result;
	}

	@Override
	public Results getAllTypes(String loginname) throws RemoteException {
		LoginUser user = FirebirdAuthorityService.getInstance().getUserByLoginName(loginname);
		List<TypeBrief> types = firebirdDocTypeManagerImpl.getPopTypesForReadDoc(user);
		Results result = new Results();
		for (TypeBrief node : types) {
			Map<String, Object> map = new HashMap<>();
			map.put("NAME", node.getName());
			map.put("ID", node.getId());
			map.put("PARENTID", node.getParentid());
			map.put("NUM", node.getNum());
			map.put("TYPE", node.getType());
			result.getList().add(map);
		}
		result.setCurrentpage(0);
		result.setPagesize(1);
		result.setTotalsize(types.size());
		return result;
	}

	@SuppressWarnings("deprecation")
	@Override
	public void runLuceneIndex(String fileid, String docid, String text) throws RemoteException {
		firebirdFileIndexManagerImpl.addFileLuceneIndex(fileid, firebirdDocManagerImpl.getDoc(docid), text);
	}

}
